﻿using System.Collections.Generic;
using System.Data.SqlClient;

namespace QWManage.DAL
{
    /// <summary>
    /// 一个用户可能属于多个部门
    /// </summary>
    internal class UsersDepartmentsRDAL : SqlDAL
    {
        public UsersDepartmentsRDAL()
        {
            TableName = "QW_Users_Departments";
        }
        public List<int> GetDepartmentsIds(string userid)
        {
            List<int> list = new List<int>();
            SqlConnection conn = new SqlConnection(SQLCON);
            try
            {
                string sql = $"select distinct DepartmentID from {TableName} where UserID=@UserID";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@UserID", userid));
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(reader.GetInt32(0));
                    }
                }
                return list;
            }
            finally
            {
                conn.Close();
            }
        }
        public List<string> GetUserIds(int departmentid)
        {
            List<string> list = new List<string>();
            SqlConnection conn = new SqlConnection(SQLCON);
            try
            {
                string sql = $"select distinct UserID from {TableName} where DepartmentID=" + departmentid;
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(reader.GetString(0));
                    }
                }
                return list;
            }
            finally
            {
                conn.Close();
            }
        }


        private void insert(string userID, int departmentID)
        {
            string sql = $"insert into {TableName} (UserID,DepartmentID) values (@UserID,@DepartmentID)";
            using (SqlConnection conn = new SqlConnection(SQLCON))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@UserID", userID));
                cmd.Parameters.Add(new SqlParameter("@DepartmentID", departmentID));
                cmd.ExecuteNonQuery();
            }
        }

        private bool Exists(string userID, int departmentID)
        {
            string sql = $"select count(UserID) from {TableName} where UserID=@UserID and DepartmentID=@DepartmentID";
            using (SqlConnection conn = new SqlConnection(SQLCON))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@UserID", userID));
                cmd.Parameters.Add(new SqlParameter("@DepartmentID", departmentID));
                int count = (int)cmd.ExecuteScalar();
                return count > 0;
            }
        }

        public void Add(Dictionary<string, List<int>> dic)
        {
            foreach (string userID in dic.Keys)
            {
                foreach (int departmentID in dic[userID])
                {
                    if (Exists(userID, departmentID)) continue;
                    insert(userID, departmentID);
                }
            }
        }
    }
}
